package clients;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class server{	
	public static void main(String [] args){
//		ServerSocket serverSocket = null;
//		InputStream is = null;
//		OutputStream os = null;
//	    PrintWriter pw = null;
//		BufferedReader br = null;
//		try {
//			serverSocket = new ServerSocket(9999);
//			// 等待客户端连接
//			Socket socket = serverSocket.accept();
//			is = socket.getInputStream();
//			//获取请求内容
//		    String info;
//		    br = new BufferedReader(new InputStreamReader(is));
//		    while ((info = br.readLine()) != null) {
//		    	os = socket.getOutputStream();
//		    	pw = new PrintWriter(new OutputStreamWriter(os));
//		    	pw.println(info);
//		    	pw.flush();
//		    }
//		    //关闭资源
//            socket.shutdownInput();
//            pw.close();
//            os.close();
//            br.close();
//            is.close();
//            socket.close();
//            serverSocket.close();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		try {
			new Thread() {
				Socket socket;
				InputStream is;
				OutputStream os;
				BufferedReader br;
				PrintWriter pw;
				ServerSocket serverSocket = new ServerSocket(9999);
				@Override
				public void run() {
					try {
						socket = serverSocket.accept();
						is = socket.getInputStream();
						//获取请求内容
					    String info;
					    br = new BufferedReader(new InputStreamReader(is));
					    while(true) {
					    	if((info = br.readLine()) != null) {
					    		os = socket.getOutputStream();
						    	pw = new PrintWriter(new OutputStreamWriter(os));
						    	pw.println(info);
						    	pw.flush();
					    	}
					    }
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}.start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
